var OTCVANamespace = window.OTCVA || (window.OTCVA = {} );
OTCVANamespace.loadingCompleted = false;

OTCVANamespace.onReady = function( server, anchor ) {
	var script = window.document.getElementById(anchor),
		 newjs = window.document.createElement('script'),
		 appjs = window.document.createElement('script');

	if( OTCVA.loadingCompleted === true ) {
		return;
	}

	// IE
	newjs.onreadystatechange = function () {
		if (newjs.readyState === 'loaded' || newjs.readyState === 'complete') {
			console.log("Not IE specific: Application loaded");
			window.document.getElementById('runMe').setAttribute('style','display: block');
			window.document.getElementById('loadMe').setAttribute('style','display: none');
		}
	};
	
	// others
	newjs.onload = function () {
		appjs.onload = function() {
			console.log("Browser: Application loaded");
			window.document.getElementById('runMe').setAttribute('style','display: block');
			window.document.getElementById('loadMe').setAttribute('style','display: none');
		};
		script = window.document.getElementById('OTCVA.MAIN.APP');
		appjs.src = 'http://' + server + ':8080/WebViewer/library/js/app.debug.js';
		script.parentNode.insertBefore(appjs);
	};

	newjs.src = 'http://' + server + ':8080/WebViewer/library/js/lib/require.js';
	newjs.setAttribute('id',"OTCVA.MAIN.APP");
	script.parentNode.insertBefore(newjs, script.nextSibling);
	OTCVANamespace.loadingCompleted = true;
	OTCVANamespace.serverName = server;
}
